package com.service.imp;

import com.mapper.UserMapper;
import com.pojo.User;
import com.service.IUserService;
import com.utils.RedisUtil;
import com.vo.UserRole;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.*;

/**
 * Created by head79 on 2017/6/21.
 */
@Service("userService")
@RunWith(SpringJUnit4ClassRunner.class)
public class UserService implements IUserService {
    private static Logger log   = LoggerFactory.getLogger(UserService.class);
    @Autowired
    private  UserMapper userMapper;
    @Autowired
    private RedisUtil redisUtil;

    public List showAllUser() {

        redisUtil.set("l","sensen");
        String sql = "select  a.*,b.* from sensen.user a join sensen.role b on a.id = b.id";
        List<Object> list = new ArrayList<Object>();
        User user = userMapper.selectByPrimaryKey(1);
        list.add(user);
        List<LinkedHashMap<String, Object>> list1 = userMapper.superSelect(sql);
        List<UserRole> listuserRole=new ArrayList<UserRole>();
        if (list1.size()>0){
            for (LinkedHashMap<String,Object> hm:list1) {
                UserRole userRole = new UserRole();
                User u = new User();
                u.setId((Integer) hm.get("id"));
                u.setUsername((String) hm.get("username"));
                u.setPassword((String) hm.get("password"));
                u.setType((String) hm.get("type"));
                userRole.setUser(u);
                userRole.setRole_group((String) hm.get("role_group"));
                userRole.setRole_name((String) hm.get("role_name"));
                listuserRole.add(userRole);
            }
        }
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) context.getBean("sqlSessionFactory");
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper1 = session.getMapper(UserMapper.class);

        log.info("取出li的key为："+redisUtil.get("l").toString());
        return listuserRole;
    }

    public Set getRoles(Object o) {
        return null;
    }

    public Object getByUsername(Object o) {
        User user = new User();
        user.setId(321);
        user.setUsername("sss");
        user.setType("teacher");
        user.setPassword("222");
        return user;
    }

    public Set getPermissions(Object o) {
        HashSet<String> set = new HashSet<String>();
        set.add("teacher");
        return set;
    }


}
